home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / FILER / TARSRC.SPK / c / disc < prev    next >
Text File  |  1994-08-23  |  5KB  |  198 lines

  1.  
  2. #include "tar.h"
  3. #include "disc.h"
  4.  
  5. static DiscRec MyDisc;
  6. static int DiscDrive;
  7. static char *DiscSpec = ":x";
  8. static int DiscSize;
  9. static char *CylinderBuffer;
  10. static int SecSize;
  11. static int TrackSize;
  12. static int CylinderSize;
  13. static int CylinderIndex;
  14. static int DiscAddress;
  15. static int Start;
  16. static int Cylinder;
  17. static int NumCylinders;
  18.  
  19.  
  20. void DescribeDisc(int Drive) {
  21.   DiscSpec[1] = Drive+'0';
  22.   chkos(os_swi2(os_X | ADFS_DescribeDisc, (int)DiscSpec, (int)&MyDisc));
  23.   DiscDrive = Drive;
  24.   DiscAddress = adfs_Offset;
  25. } /* DescribeDisc */
  26.  
  27.  
  28. void InitDisc() {
  29.   DiscAddress = 0;
  30.   Cylinder = 0;
  31.   CylinderIndex = 0;
  32.   Start = 1;
  33. } /* InitDisc */
  34.  
  35.  
  36. void SetupDisc(int Drive, char Format) {
  37.   NumCylinders = 80;
  38.   DiscSpec[1] = Drive+'0';
  39.   MyDisc.Density = 2;
  40.   MyDisc.NumHeads = 2;
  41.   MyDisc.LowSector = 0;
  42.   switch (Format) {
  43.     case 'E':
  44.     case 'D':
  45.       MyDisc.Log2SecSize = 10;
  46.       MyDisc.SecsPerTrack = 5;
  47.       break;
  48.     case 'F':
  49.       MyDisc.Log2SecSize = 10;
  50.       MyDisc.SecsPerTrack = 10;
  51.       MyDisc.Density = 4;
  52.       break;
  53.     case 'L':
  54.       MyDisc.Log2SecSize = 8;
  55.       MyDisc.SecsPerTrack = 16;
  56.       break;
  57.     case 'M':
  58.       MyDisc.LowSector = 1;
  59.       MyDisc.Log2SecSize = 9;
  60.       MyDisc.SecsPerTrack = 9;
  61.       break;
  62.     case 'Q':
  63.       MyDisc.LowSector = 1;
  64.       MyDisc.Log2SecSize = 9;
  65.       MyDisc.SecsPerTrack = 18;
  66.       MyDisc.Density = 4;
  67.       break;
  68.     case 'H':
  69.       MyDisc.LowSector = 1;
  70.       MyDisc.Log2SecSize = 9;
  71.       MyDisc.SecsPerTrack = 15;
  72.       break;
  73.     case 'N':
  74.       MyDisc.LowSector = 1;
  75.       NumCylinders = 40;
  76.       MyDisc.Log2SecSize = 9;
  77.       MyDisc.SecsPerTrack = 9;
  78.       break;
  79.   }
  80.   SecSize = 1 << MyDisc.Log2SecSize;
  81.   MyDisc.DiscSize =
  82.     (long)SecSize * (long)MyDisc.SecsPerTrack *
  83.     (long)MyDisc.NumHeads * (long)NumCylinders;
  84.   TrackSize = SecSize * MyDisc.SecsPerTrack;
  85.   CylinderSize = TrackSize * MyDisc.NumHeads;
  86.   DiscSize = CylinderSize * NumCylinders;
  87.   DiscDrive = Drive;
  88.   CylinderBuffer = (char *)malloc(CylinderSize);
  89.   DiscAddress = 0;
  90.   Cylinder = 0;
  91.   CylinderIndex = 0;
  92.   Start = 1;
  93.   DiscNo = 1;
  94. } /* SetupDisc */
  95.  
  96.  
  97. void CylinderOp(int Operation) {
  98.   int Head;
  99.   os_regset regs;
  100.  
  101.   for (Head = 0; Head < MyDisc.NumHeads; Head++) {
  102.     do {
  103.       regs.r[1] = Operation | (1 << 6) | ((int)&MyDisc << 6);
  104.       regs.r[2] = (SecSize*(MyDisc.SecsPerTrack
  105.                                       *(MyDisc.NumHeads*Cylinder+Head)))
  106.                   | (DiscDrive << 29);
  107.       regs.r[3] = (int)CylinderBuffer+Head*TrackSize;
  108.       regs.r[4] = TrackSize;
  109.     } while (!chkos(os_swix(ADFS_DiscOp,®s)));
  110.   }
  111.   Cylinder++;
  112.   CylinderIndex = 0;
  113.   Start = 0;
  114. } /* CylinderOp */
  115.  
  116.  
  117. int WriteDisc(char *Buffer, long Size) {
  118.   int ToWrite, ToCopy;
  119.  
  120.   ToWrite = (int)Size;
  121.   do {
  122.     ToCopy = CylinderSize - CylinderIndex;
  123.     if (ToCopy > ToWrite)
  124.       ToCopy = ToWrite;
  125.     memcpy(CylinderBuffer + CylinderIndex, Buffer, ToCopy);
  126.     Buffer += ToCopy;
  127.     CylinderIndex += ToCopy;
  128.     ToWrite -= ToCopy;
  129.     if (CylinderIndex >= CylinderSize)
  130.       CylinderOp(adfs_WriteSectors);
  131.   } while (ToWrite > 0);
  132.   DiscAddress += (int)Size;
  133.   return((int)Size);
  134. } /* WriteDisc */
  135.  
  136.  
  137. int ReadDisc(char *Buffer, long Size) {
  138.   int ToRead, ToCopy;
  139.  
  140.   ToRead = (int)Size;
  141.   do {
  142.     if (!Start) {
  143.       ToCopy = CylinderSize - CylinderIndex;
  144.       if (ToCopy > ToRead)
  145.         ToCopy = ToRead;
  146.       memcpy(Buffer,CylinderBuffer+CylinderIndex,ToCopy);
  147.       Buffer += ToCopy;
  148.       CylinderIndex += ToCopy;
  149.       ToRead -= ToCopy;
  150.     }
  151.     if (ToRead > 0 && CylinderIndex >= CylinderSize || Start) {
  152.       if (Cylinder < NumCylinders) {
  153.         CylinderOp(adfs_ReadSectors);
  154.       } else {
  155.         fprintf(stderr,"Please insert disc #%d:",++DiscNo);
  156.         while (getchar() != '\n')
  157.           ;
  158.         InitDisc();
  159.         CylinderOp(adfs_ReadSectors);
  160.         CylinderIndex += RECORDSIZE;
  161.         DiscAddress += RECORDSIZE;
  162.       }
  163.     }
  164.   } while (ToRead > 0);
  165.   DiscAddress += (int)Size;
  166.   return((int)Size);
  167. } /* ReadDisc */
  168.  
  169.  
  170. long FreeOnDisc(void) {
  171.   return((long)DiscSize-(long)DiscAddress);
  172. } /* StillOnDisc */
  173.  
  174.  
  175. void FlushDisc(void) {
  176.   if (CylinderIndex > 0)
  177.     CylinderOp(adfs_WriteSectors);
  178. } /* FlushDisc */
  179.  
  180.  
  181. void BackDisc(void) {
  182.   int NumBytesBack,NumCylindersBack,NewDiscAddress;
  183.  
  184.   NumBytesBack = NumBlocksRead*RECORDSIZE;
  185.   NumCylindersBack = (NumBytesBack+CylinderSize-1)/CylinderSize;
  186.   Cylinder -= NumCylindersBack;
  187.   CylinderOp(adfs_ReadSectors);
  188.   Cylinder--;
  189.   NewDiscAddress = Cylinder*CylinderSize;
  190.   CylinderIndex = DiscAddress-NumBytesBack-NewDiscAddress;
  191.   DiscAddress = NewDiscAddress;
  192. } /* BackDisc */
  193.  
  194.  
  195. void FormatDisc(void) {
  196.   os_cli("adfs:format 0 e y");
  197. } /* FormatDisc */
  198.